package com.bazhua.bazo.service.Impl;

import com.alibaba.fastjson.JSON;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bazhua.bazo.repository.entity.BannerInfo;
import com.bazhua.bazo.repository.mapper.BannerInfoMapper;
import com.bazhua.bazo.repository.model.BannerSearch;
import com.bazhua.bazo.repository.model.result.*;
import com.bazhua.bazo.service.IBannerService;
import com.bazhua.bazo.service.ICommonService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.Date;
import java.util.List;


@Service
public class BannerServiceImpl extends ServiceImpl<BannerInfoMapper, BannerInfo> implements IBannerService {

    @Autowired
    private ICommonService commonService;

    @Override
    public CommonResult GetBannerList() {
        QueryWrapper<BannerInfo> wapper = new QueryWrapper<>();
        wapper.orderByDesc("create_time");
        List<BannerInfo> list = baseMapper.selectList(wapper);
        if (list != null && list.size() > 0)
            return ResultUtils.ok(list);
        else
            return ResultUtils.error(BaseResultCode.Have_Not_Data);
    }

    @Override
    public CommonResult UploadBanner(String fileName, InputStream stream) {
        String extions = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
        if (!extions.equals("jpg") && !extions.equals("png") && !extions.equals("jpeg"))
            return ResultUtils.error(90050, "必须上传图片");

        CommonResult oss = commonService.upLoadPicture(fileName, stream);
        if (oss.getCode().equals(0)) {
            BannerInfo banner = new BannerInfo();
            banner.setBannerName(fileName.substring(0, fileName.lastIndexOf(".") - 1));
            String fullUrl = oss.getData().toString();
            banner.setBannerUrl(fullUrl);
            banner.setCreateTime(new Date());
            baseMapper.insert(banner);
            return ResultUtils.ok(fullUrl);
        } else
            return oss;
    }

    public CommonResult saveBanner(BannerInfo bannerInfo) {
        if(bannerInfo.getId() == null || bannerInfo.getId() == 0){
            bannerInfo.setCreateTime(new Date());
            baseMapper.insert(bannerInfo);
        }else {
            baseMapper.updateById(bannerInfo);
        }
        return ResultUtils.ok();
    }

    @Override
    public CommonResult DeleteBanner(List<Long> bannerIds) {
        baseMapper.deleteBatchIds(bannerIds);
        return ResultUtils.ok();
    }

    @Override
    public PageListResult GetBannerPage(BannerSearch search) {
        Page<BannerInfo> page = new Page<>();
        page.setSize(search.getPageSize());
        page.setPages(search.getPageNum());
        QueryWrapper<BannerInfo> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        if (search.getBannerName() != null && !StringUtils.isBlank(search.getBannerName()))
            wrapper.like("banner_name", search.getBannerName());
        page = baseMapper.selectPage(page, wrapper);
        return ResultUtils.pageOk(page);
    }
}
